<template>
  <div id="globalheader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.jpg" alt="logo" />
            <div class="title">NUAA在线图库</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-avatar class="avatar" :src="loginUserStore.loginUser.userAvatar" />
            <a-dropdown>
              <a class="ant-dropdown-link" @click.prevent>
                {{ loginUserStore.loginUser.userName ?? '无名' }}
                <DownOutlined />
              </a>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="changeUser">个人中心</a-menu-item>
                  <a-menu-item key="1" @click="onClick">退出登录</a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { DownOutlined, HomeOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingPost } from '@/api/userController.ts'

const router = useRouter()
const loginUserStore = useLoginUserStore()
// 保证刷新后，不会退出登录
loginUserStore.getLoginUser()
// 退出登录
const onClick: MenuProps['onClick'] = async () => {
  const res = await userLogoutUsingPost()
  if (res.data.code === 0) {
    loginUserStore.deleteLoginUser()
    message.success('退出成功')
    await router.replace('/user/login')
  } else {
    message.error('退出失败,' + res.data.message)
  }
}
// 菜单
const current = ref<string[]>(['home'])

const orginMenus = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/add_picture',
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/pictureManage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/admin/spaceManage',
    label: '空间管理',
    title: '空间管理',
  },
  {
    key: 'others',
    label: h('a', { href: 'http://111.229.146.147:7070/', target: '_blank' }, 'NUAA智能测评'),
    title: '编程导航',
  },
]

const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu: any) => {
    if (menu.key?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}
const items = computed(() => {
  return filterMenus(orginMenus)
})
// 菜单跳转
const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}
router.afterEach((to) => {
  current.value = [to.path]
})
const changeUser = () => {
  router.push('/user/centre')
}
</script>
<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}

.avatar {
  margin-right: 5px;
}
</style>
